package demozkdiscovery;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;


@RefreshScope
@RestController
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class DemoZkDiscoveryApplication {


	@Autowired
	private HelloServiceFeign helloService;

	/**
	 * how to set key/value in zookeeper?
	 *
	 * for all application, if spring.cloud.zookeeper.config.defaultContext set to application(default) use:
	 * set /config/application/test.value 999999999999
	 * set /config/application,dev/test.value 999999999999
	 *
	 *
	 * default, not profile active:
	 * set /config/hello/test.value 999999999999
	 *
	 * dev profile active
	 * set /config/hello,dev/test.value 999999999999
	 *
	 * from bottom to top order:
	 * /config/hello,dev
	 * /config/hello
	 * /config/application,dev
	 * /config/application
	 */
	@Value("${test.value}")
	private String value;

	@Value("${test.abc}")
	private String abc;


	@Value("${test.dd}")
	private String dd;


	public static final String ID = UUID.randomUUID().toString();


	@GetMapping("/hello")
	public ResponseEntity<String> hello() {
		System.out.println(ID);
		return ResponseEntity.ok(value);
	}

	@GetMapping("/dd")
	public ResponseEntity<String> dd() {
		return helloService.dd();
	}


	@GetMapping("/test")
	public ResponseEntity<String> test() {
		return helloService.hello();
	}

	public static void main(String[] args) {
		SpringApplication.run(DemoZkDiscoveryApplication.class, args);
	}

}
